package com.bwhx.eruler.controller;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bwhx.eruler.bean.QRCode;
import com.bwhx.eruler.service.WXService;
import com.bwhx.eruler.util.Constant;
import com.bwhx.eruler.util.HttpUtil;
import com.fasterxml.jackson.databind.ObjectMapper;

import net.sf.json.JSONObject;

@Controller
public class WCController extends BaseController {
	
	@Autowired
	WXService wxService;
	
	@ResponseBody
	@RequestMapping(value = "/page.everyone.getQRCode.ashx")
	public HashMap<String,Object> saveOrUpdateClothes(HttpServletRequest req) throws Exception {
		
		InputStream in = req.getInputStream();
		 
		 //这里接受到客户端传过来的数据 是json字符类型
		BufferedReader reader = new BufferedReader(new InputStreamReader(in,"UTF-8"));
	    StringBuilder sb = new StringBuilder();
	    String line;
	    while((line=reader.readLine())!=null){
	        sb.append(line);
	    }
	    String param = sb.toString();
		
	    ObjectMapper objectMapper = new ObjectMapper();;
	    LinkedHashMap<String, Object> map = objectMapper.readValue(param, LinkedHashMap.class);
	    for(Map.Entry<String, Object> entry:map.entrySet()){
	    	req.setAttribute(entry.getKey(), entry.getValue());
	    }	
	    
	    
	    
	    
	    
		HashMap<String,Object> resultMap = new HashMap<String,Object>();
		String phone = (String) req.getAttribute("phone");
		
		Pattern pattern = Pattern.compile("^1\\d{10}"); 
	    Matcher matcher = pattern.matcher(phone);
		  
		
		if(matcher.find()){
			QRCode qr = wxService.getByPhone(phone);
			Date date = new Date();
			
			if(null==qr){
				qr = new QRCode();
				Long sceneid = wxService.getMaxSceneid();
				String url = getFromWXServer(sceneid+1);
				
				qr.setCreatetime(date);
				qr.setMobilephone(phone);
				qr.setSceneid(sceneid+1);
				qr.setSceneurl(url);
				
				wxService.insertQRCodeInfo(qr);
				
			}else if(new Date().getTime()/1000 - qr.getCreatetime().getTime()/1000>2592000){
				
				
				String url = getFromWXServer(qr.getSceneid());
				
				qr.setCreatetime(date);
				qr.setSceneurl(url);
				
				wxService.updateQRCodeInfo(qr);
			}
			
			resultMap.put("status", "1");
			resultMap.put("msg", "获取成功！");
			resultMap.put("qrcode", qr.getSceneurl());
			
		}else{
			resultMap.put("status", "0");
			resultMap.put("msg", "手机号为11位阿拉伯数字，请确认输入是否正确。");
			
		}
	
		
		
	    return resultMap;  
	  }

	public String getFromWXServer(long sceneid) {
		String result = HttpUtil.sendGet(Constant.WX_SERVER+"QRCodeServlet?sceneid="+sceneid);
		log.info(result);
		JSONObject jsonObj = JSONObject.fromObject(result);
		String url = jsonObj.getString("url");
		return url;
	}

	
	
	
 
}
